AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Use(ply, caller)
	local gun = ply:GetActiveWeapon()
	if !self.dt.Sparking then
		if(gun.Upgrade ~= nil) then
			local upgradecost = math.random(120,250)
			if(!ply:CanAfford(upgradecost)) then 
				Notify(ply, 4, 3, "You cant afford that!")
				self.dt.Sparking = true
				timer.Create(tostring(self), 0.20, 1, function() self.dt.Sparking = false end)
				return
			end
			if gun:GetUpgrade() > 0 then
				Notify(ply, 4, 3, "This weapon is already upgraded!")
				self.dt.Sparking = true
				timer.Create(tostring(self), 0.20, 1, function() self.dt.Sparking = false end)
			else
				gun:SetUpgrade(1)
				Notify(ply, 2, 3, string.format("Your %s has been upgraded for a cost of $%s", gun.PrintName, upgradecost))
				Notify(ply, 3, 3, gun.Upgradestring)
				ply:TakeMoney(upgradecost)
				self.dt.Sparking = true
				timer.Create(tostring(self), 1, 1, function() self.dt.Sparking = false end)
			end	
		else
			Notify(ply, 4, 3, "You cant upgrade that!")
			self.dt.Sparking = true
			timer.Create(tostring(self), 0.20, 1, function() self.dt.Sparking = false end)
		end
	end
end

function ENT:OnRemove()
	timer.Remove(tostring(self))
end
